﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace LibSystem
{
    public partial class managerForm : Form
    {
        private MySqlConnection myconn;
        public managerForm(MySqlConnection conn)
        {
            myconn = conn;
            InitializeComponent();
        }

        private void managerForm_Load(object sender, EventArgs e)
        {
            switch (this.Text)
            {
                case "修改图书":
                    break;
                case "添加图书":
                    this.button2.Enabled = false;
                    this.button2.Visible = false;
                    break;
                case "删除图书":
                    this.textBox2.ReadOnly = true;
                    this.textBox3.ReadOnly = true;
                    this.textBox4.ReadOnly = true;
                    this.textBox5.ReadOnly = true;
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
                try
                {
                    String mystr = "select * from book where BookNum = " + textBox1.Text + ";";
                    MySqlCommand mycmd = new MySqlCommand(mystr, myconn);
                    MySqlDataReader myreader = mycmd.ExecuteReader();
                    myreader.Read();
                    this.textBox2.Text = myreader[1].ToString();
                    this.textBox3.Text = myreader[2].ToString();
                    this.textBox4.Text = myreader[3].ToString();
                    this.textBox5.Text = ((DateTime)myreader[5]).ToString("yyyy-MM-dd");
                myreader.Close();
                }
                catch(Exception err)
                {
                    MessageBox.Show("err", "cuowu", MessageBoxButtons.OK);
                }
                
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str;
            MySqlCommand mycmd;
            MySqlDataReader myreader;
            switch (this.Text)
            {
                case "修改图书":
                    str = "update book set BookName = '"+textBox2.Text+"',Writer= '"+textBox3.Text+"',Publisher= '"
                        +textBox4.Text+"',PubDate= '"+textBox5.Text+"' where BookNum="+textBox1.Text+";";
                    mycmd = new MySqlCommand(str, myconn);
                    myreader = mycmd.ExecuteReader();
                    myreader.Read();
                    MessageBox.Show("修改成功", "Succeed!", MessageBoxButtons.OK);
                    myreader.Close();
                    break;
                case "添加图书":
                    str = "insert into book (BookNum,BookName,Writer,Publisher,PubDate) values ("
                        +textBox1.Text+",'"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"');";
                    mycmd = new MySqlCommand(str, myconn);
                    myreader = mycmd.ExecuteReader();
                    myreader.Read();
                    MessageBox.Show("添加成功", "Succeed!", MessageBoxButtons.OK);
                    myreader.Close();
                    break;
                case "删除图书":
                    str = "delete from book where BookNum = "+textBox1.Text+";";
                    mycmd = new MySqlCommand(str, myconn);
                    myreader = mycmd.ExecuteReader();
                    myreader.Read();
                    MessageBox.Show("删除成功", "Succeed!", MessageBoxButtons.OK);
                    myreader.Close();
                    break;
            }
        }
    }
}
